﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CookComputing.XmlRpc;
using System.Reflection;
using System.Collections;
using Microsoft.Phone.Controls;
using System.IO;

namespace HomeWP
{
    public partial class HMWeather : UserControl
    {

        public HMWeather()
        {
            InitializeComponent();
        }

        public Homematic proxy;
        public string Address { get; set; }
        public string DisplayName { get; set; }
        public string CCUIP { get; set; }
        public int CCUPort { get; set; }
        public Device HMDevice { get; set; }
        private bool isLoaded = true;

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            _dispName.Text = DisplayName;

            proxy = new Homematic();
            if (!string.IsNullOrEmpty(CCUIP))
            {
                proxy.Url = "http://" + CCUIP + ":" + CCUPort;
                proxy.Timeout = 10000;
            }
            proxy.GetValue(Address, this);

            if (((App)App.Current).HMData.IsFavorit(Address))
            {
                mi_Favorit.Visibility = System.Windows.Visibility.Collapsed;
                mi_DelFavorit.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                mi_Favorit.Visibility = System.Windows.Visibility.Visible;
                mi_DelFavorit.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        [XmlRpcUrl(Homematic.UrlCCU)]
        public class Homematic : XmlRpcClientProtocol
        {
            //Default URL
            public const string UrlCCU = "http://192.168.2.15:2001";

            [XmlRpcEnd]
            public object EndGetValue(IAsyncResult iasr)
            {
                return this.EndInvoke(iasr);
            }

            [XmlRpcBegin("getValue")]
            internal IAsyncResult getValue(string address, string valueKey, AsyncCallback acb)
            {
                return this.BeginInvoke(MethodBase.GetCurrentMethod(), new object[] { address, valueKey }, acb, null);
            }

            [XmlRpcBegin("setValue")]
            internal IAsyncResult setValue(String address, String valueKey, ValueType value, AsyncCallback acb)
            {
                return this.BeginInvoke(MethodBase.GetCurrentMethod(), new object[] { address, valueKey, value }, acb, null);
            }

            public void GetValue(string Address, HMWeather oBox)
            {
                getValue(Address, "TEMPERATURE", asr =>
                {
                    ((App)App.Current).RootFrame.Dispatcher.BeginInvoke(delegate()
                    {
                        try
                        {
                            var posts = EndGetValue(asr);
                            string sResult = posts.ToString();
                            try
                            {
                                oBox._temp.Text = sResult + "°C";
                            }
                            catch { }

                        }
                        catch (Exception ex)
                        {
                            ex.Message.ToString();
                        }
                    });

                });

                getValue(Address, "HUMIDITY", asr =>
                {
                    ((App)App.Current).RootFrame.Dispatcher.BeginInvoke(delegate()
                    {
                        try
                        {
                            var posts = EndGetValue(asr);
                            string sResult = posts.ToString();
                            try
                            {
                                oBox._hum.Text = sResult + "%";
                            }
                            catch { }

                        }
                        catch (Exception ex)
                        {
                            ex.Message.ToString();
                        }
                    });

                });

                try
                {
                    if (oBox.HMDevice.ParentType.StartsWith("HM-CC-TC"))
                    {
                        getValue(Address.Split(':')[0] + ":2", "SETPOINT", asr =>
                        {
                            ((App)App.Current).RootFrame.Dispatcher.BeginInvoke(delegate()
                            {
                                try
                                {
                                    var posts = EndGetValue(asr);
                                    string sResult = posts.ToString();
                                    try
                                    {
                                        if (!string.IsNullOrEmpty(sResult))
                                        {
                                            oBox._tb1.Visibility = Visibility.Visible;
                                            oBox._tb1value.Visibility = Visibility.Visible;
                                            oBox._tb1value.Text = sResult + "°C";
                                        }
                                    }
                                    catch { }

                                }
                                catch (Exception ex)
                                {
                                    ex.Message.ToString();
                                }
                            });

                        });
                    }
                }
                catch { }
            }
        }


        private void mi_DelFavorit_Click(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).HMData.DeleteFavorit(Address);
        }

        private void mi_Favorit_Click(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).HMData.AddFavorit(Address);
        }

    }
}
